1   /*
2    * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
3    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4    *
5    * This code is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU General Public License version 2 only, as
7    * published by the Free Software Foundation.
8    *
9    * This code is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12   * version 2 for more details (a copy is included in the LICENSE file that
13   * accompanied this code).
14   *
15   * You should have received a copy of the GNU General Public License version
16   * 2 along with this work; if not, write to the Free Software Foundation,
17   * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18   *
19   * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20   * or visit www.oracle.com if you need additional information or have any
21   * questions.
22   */
23  
24  /*
25   * @test
26   * @bug 6623219
27   * @summary Test canDisplayUpTo with supplementary characters.
28   */
29  
30  import java.awt.*;
31  import java.text.*;
32  
33  public class SupplementaryCanDisplayUpToTest {
34      // Lists consisting of a font name, test text, and its expected
35      // return value. Test text uses private area code point U+F0000
36      // (\udb80\udc00).
37      private static String[][] DATA = {
38          // Windows
39          { "Meiryo", "\ud87e\udd45\ud87e\udd47\udb80\udc00", "4" },
40          { "Meiryo", "\ud87e\udd45\ud87e\udd47\udb80Z", "4" },
41          { "Meiryo", "\ud87e\udd45\ud87e\udd47\udb80", "4" },
42          { "Meiryo", "\ud87e\udd45\ud87e\udd47\udc00", "4" },
43          { "Meiryo", "\ud87e\udd45\ud87e\udd47", "-1" },
44  
45          // Linux
46          { "AR PL UMing TW", "\ud87e\udc25\ud87e\udc3b\udb80\udc00", "4" },
47          { "AR PL UMing TW", "\ud87e\udc25\ud87e\udc3b\udb80Z", "4" },
48          { "AR PL UMing TW", "\ud87e\udc25\ud87e\udc3b\udb80", "4" },
49          { "AR PL UMing TW", "\ud87e\udc25\ud87e\udc3b\udc00", "4" },
50          { "AR PL UMing TW", "\ud87e\udc25\ud87e\udc3b", "-1" },
51  
52          // Solaris
53          { "FZMingTi", "\ud87e\udc25\ud87e\udc3b\udb80\udc00", "4" },
54          { "FZMingTi", "\ud87e\udc25\ud87e\udc3b\udb80Z", "4" },
55          { "FZMingTi", "\ud87e\udc25\ud87e\udc3b\udb80", "4" },
56          { "FZMingTi", "\ud87e\udc25\ud87e\udc3b\udc00", "4" },
57          { "FZMingTi", "\ud87e\udc25\ud87e\udc3b", "-1" },
58      };
59      private static int errorcount = 0;
60  
61      public static void main(String[] args) {
62          for (String[] data : DATA) {
63              String fontname = data[0];
64              Font font = new Font(fontname, Font.PLAIN, 16);
65              if (font.getFamily().equals(Font.DIALOG)) {
66                  // Skip any unavailable fonts.
67                  continue;
68              }
69  
70              System.out.printf("Testing with font '%s'... ", fontname);
71              int errors = 0;
72              String text = data[1];
73              int expected = Integer.parseInt(data[2]);
74  
75              int result = font.canDisplayUpTo(text);
76              if (result != expected) {
77                  System.err.println("canDisplayUpTo(String) returns " + result);
78                  errors++;
79              }
80  
81              result = font.canDisplayUpTo(text.toCharArray(), 0, text.length());
82              if (result != expected) {
83                  System.err.println("canDisplayUpTo(char[], int, int) returns " + result);
84                  errors++;
85              }
86  
87              CharacterIterator iter = new StringCharacterIterator(text);
88              result = font.canDisplayUpTo(iter, iter.getBeginIndex(), iter.getEndIndex());
89              if (result != expected) {
90                  System.err.println("canDisplayUpTo(CharacterIterator, int, int) returns " + result);
91                  errors++;
92              }
93  
94              if (errors == 0) {
95                  System.out.println("passed");
96              } else {
97                  System.out.println("failed");
98                  errorcount += errors;
99              }
100         }
101         if (errorcount > 0) {
102             throw new RuntimeException("SupplementaryCanDisplayUpToTest: failed");
103         }
104     }
105 }